package zeke.com.camera.activity;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;

import zeke.com.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;

public class ImageUploadActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.imageupload);

		Button imageupload = (Button) findViewById(R.id.imageupload);

		imageupload.setOnClickListener(new ImageUploadListener());
	}

	private class ImageUploadListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			File file = new File(Environment.getExternalStorageDirectory(),
					"/xxxxxxxxx.jpg");
			try {
				sendData(file);
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void sendData(File file) throws ClientProtocolException, IOException {

		// URL url = new
		// URL("http://192.168.0.195:8080/testjson/testjson.test");

		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

		HttpPost httppost = new HttpPost("http://192.168.0.195:8080/testjson/testjson.test");

		FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");

		httppost.setEntity(reqEntity);
		reqEntity.setContentType("binary/octet-stream");
		System.out.println("executing request " + httppost.getRequestLine());
		HttpResponse response = httpclient.execute(httppost);
		
//		HttpEntity resEntity = response.getEntity();
//
//		System.out.println(response.getStatusLine());
//		if (resEntity != null) {
//			System.out.println(EntityUtils.toString(resEntity));
//		}
//		if (resEntity != null) {
//			resEntity.consumeContent();
//		}

		httpclient.getConnectionManager().shutdown();

	}

}
